
import React, { useCallback } from 'react';
import update from 'immutability-helper';
import CommonController from '../CommonController';

import { IProps } from '../config';

const SwitchController = ({
  tableProps,
  print,
  required,
  originalIndex,
  columns,
  dataIndex,
  title,
  titleEn,
  width,
  disabled,
  exportFlag,
  changeColumnOptions,
  changeControlProps,
  setCurColumn,
  fontSize,
  fontWeight,
  backgroundColor,
  color,
  customizeStyleFlag
}: IProps) => {
  const onChangeColumnOptions = useCallback(
    (e: any, k: string, isChecked?: boolean) => {
      changeColumnOptions(k, isChecked ? e.target.checked : e.target.value, originalIndex);
    },
    [changeColumnOptions, originalIndex]
  );

  const _changeControlProps = useCallback(
    (e: any, k: string, isChecked?: boolean) => {
      changeControlProps(
        'tableProps',
        {
          ...tableProps,
          columns: update(columns!, {
            [originalIndex!]: {
              [k]: {
                $set: isChecked ? e.target.checked : e.target.value,
              },
            },
          }),

        },
        true
      );
      setCurColumn?.({ ...columns![originalIndex!], originalIndex, [k]: isChecked ? e.target.checked : e.target.value, });
    },
    [changeControlProps, tableProps, columns, originalIndex, setCurColumn]
  );

  return (
    <CommonController
      onChangeControlProps={_changeControlProps}
      onChangeColumnOptions={onChangeColumnOptions}
      {...{ title, titleEn, dataIndex, width, required, print, disabled, exportFlag, fontSize, fontWeight, backgroundColor, color, customizeStyleFlag }}
    >
    </CommonController>
  );
};

export default SwitchController;
